<?php
namespace app\libs;

class Speak
{
    function xfyun($file, $text)
    {
        $daytime = strtotime('1970-1-1T00:00:00 UTC');
        $api = "http://api.xfyun.cn/v1/service/v1/ise";
        $XAppid = "e060a092";
        $Apikey = "6d18513e6984727caff6d2be443c3f79";
        $XCurTime = time();
        $XParam = "";
        $XCheckSum = "";

        $param = [
            'language' => 'en_us',
            'aue' => 'raw',
            'category' => 'read_sentence'
        ];
        $cur_time = (string)time();
        $x_param = base64_encode(json_encode($param));
        $header_data = [
            'X-Appid:'.$XAppid,
            'X-CurTime:'.$cur_time,
            'X-Param:'.$x_param,
            'X-CheckSum:'.md5($Apikey.$cur_time.$x_param),
            'Content-Type:application/x-www-form-urlencoded; charset=utf-8'
        ];

        //Body
        $file_path = $file;
        $file_content = file_get_contents($file_path);
        $body_data = 'audio='.urlencode(base64_encode($file_content)).'&text='.urlencode($text);

        $url = "http://api.xfyun.cn/v1/service/v1/ise";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body_data);

        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}

?>